package net.tngou.crawler.action;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.lang3.math.NumberUtils;
import org.bouncycastle.jcajce.provider.asymmetric.dsa.DSASigner.stdDSA;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import net.tngou.crawler.enums.StateEnum;
import net.tngou.crawler.enums.ValidEnum;
import net.tngou.crawler.pojo.Data;
import net.tngou.crawler.pojo.Ml;
import net.tngou.crawler.pojo.Outvalueurl;
import net.tngou.crawler.pojo.Tag;
import net.tngou.crawler.pojo.Urldata;
import net.tngou.crawler.pojo.Web;
import net.tngou.crawler.pojo.Weburl;
import net.tngou.crawler.service.DataService;
import net.tngou.crawler.service.MlService;
import net.tngou.crawler.service.TagService;
import net.tngou.crawler.util.StringUtil;


/**
 * 
 * @author tngou@tngou.net
 *
 */
public class ApiAction extends BaseAction {

	private static final Logger log= LoggerFactory.getLogger(ApiAction.class);
	/**
	 * 更新网站
	 */
	public void updateweb() {
		Web web =getAsk(Web.class);
		if(web.getId()>0)
		{
			Web bean =new Web().get(web.getId());
			if(bean==null) {
				run_false("请求数据错误！");return;
			}
			if(StringUtil.isNotEmpty(web.getName()))bean.setName(web.getName());
			if(StringUtil.isNotEmpty(web.getUrl()))bean.setUrl(web.getUrl());
			if(web.getNext_t()!=null)bean.setNext_t(web.getNext_t());
			if(web.getType()>0||web.getType()!=bean.getType())bean.setType(web.getType());
			if(web.getIsuse()>0||web.getIsuse()!=bean.getIsuse())bean.setIsuse(web.getIsuse());
			if(web.getSpace()>0||web.getSpace()!=bean.getSpace())bean.setSpace(web.getSpace());		
			long id = bean.update();
			if(id>0)
			{
				run_success("更新成功！");return;
			}else
			{
				run_false("更新失败！");return;
			}
			
		}else
		{
			if(isEmpty(web.getName()))
			{
				run_false("请求参数错误！");return;
			}
			long id = web.save();
			if(id>0) {
				run_success("添加成功！");return;
			}else
			{
				run_false("添加失败！");return;
			}
		}	
	}
	
	public void deleteweb() {
		Web web =new Web().get(ask.getId());
		if(web==null)
		{
			run_false("数据不存在！");return;
		}
		web.delete();
		String filter="web="+ask.getId();
		new Weburl().delete(filter);
		new Outvalueurl().delete(filter);
		run_success("删除成功！");
		
	}
	
	/**
	 * 更新目标网址
	 */
	public void updateweburl() {
		Weburl weburl =getAsk(Weburl.class);
		if(weburl.getId()>0)
		{
			Weburl bean =new Weburl().get(weburl.getId());
			if(bean==null) {
				run_false("请求数据错误！");return;
			}
		
			if(StringUtil.isNotEmpty(weburl.getUrl()))bean.setUrl(weburl.getUrl());
			if(StringUtil.isNotEmpty(weburl.getKey_value()))bean.setKey_value(weburl.getKey_value());
			if(StringUtil.isNotEmpty(weburl.getInvalid_data()))bean.setInvalid_data(weburl.getInvalid_data());
			if(StringUtil.isNotEmpty(weburl.getUnique_value()))bean.setUnique_value(weburl.getUnique_value());
			long id = bean.update();
			if(id>0)
			{
				run_success("更新成功！");return;
			}else
			{
				run_false("更新失败！");return;
			}
			
		}else
		{
			if(isEmpty(weburl.getUrl())||weburl.getWeb()==0)
			{
				run_false("请求参数错误！");return;
			}
			long id = weburl.save();
			if(id>0) {
				run_success("添加成功！");return;
			}else
			{
				run_false("添加失败！");return;
			}
		}	
	}
	
	public void deleteweburl() {
		Weburl bean =new Weburl().get(ask.getId());
		if(bean==null)
		{
			run_false("数据不存在！");return;
		}
		bean.delete();
		run_success("删除成功！");
	}

	
	/**
	 * 更新目标网址
	 */
	public void updateoutvalueurl() {
		Outvalueurl outvalueurl =getAsk(Outvalueurl.class);
		if(outvalueurl.getId()>0)
		{
			Outvalueurl bean =new Outvalueurl().get(outvalueurl.getId());
			if(bean==null) {
				run_false("请求数据错误！");return;
			}
		
			if(StringUtil.isNotEmpty(outvalueurl.getUrl()))bean.setUrl(outvalueurl.getUrl());
			if(outvalueurl.getType()>0||outvalueurl.getType()!=bean.getType())bean.setType(outvalueurl.getType());
			long id = bean.update();
			if(id>0)
			{
				run_success("更新成功！");return;
			}else
			{
				run_false("更新失败！");return;
			}
			
		}else
		{
			if(isEmpty(outvalueurl.getUrl())||outvalueurl.getWeb()==0)
			{
				run_false("请求参数错误！");return;
			}
			long id = outvalueurl.save();
			if(id>0) {
				run_success("添加成功！");return;
			}else
			{
				run_false("添加失败！");return;
			}
		}	
	}
	public void deleteoutvalueurl() {
		Outvalueurl bean =new Outvalueurl().get(ask.getId());
		if(bean==null)
		{
			run_false("数据不存在！");return;
		}
		bean.delete();
		run_success("删除成功！");
	}

	/**
	 * 更新系统
	 * @throws ConfigurationException 
	 */
	public void sys() throws ConfigurationException {
		Map<String, ? extends Object> map = request.getParameterMap();
		Parameters params = new Parameters();
		FileBasedConfigurationBuilder<FileBasedConfiguration> builder = new FileBasedConfigurationBuilder<FileBasedConfiguration>(
				PropertiesConfiguration.class).configure(params.properties().setFileName("info.properties"));
		Configuration config = builder.getConfiguration();

//		config.setProperty("versionName", version);
//		config.setProperty("url", url);
//		config.setProperty("description", description);
		Set<String> keys = map.keySet();
		keys.forEach(e->{
			if(config.getString(e)!=null)config.setProperty(e, map.get(e));
		});
		
		builder.save();
		run_success("更新成功！");
	}
	
	/**
	 * 数据学习
	 */
	public void studydata() {
		Data bean =new Data().get(ask.getId());
		if(bean==null)
		{
			run_false("数据不存在！");return;
		}
		MlService service = new MlService();
		service.study(bean);
		run_success("完成！");				
	}
	
	/**
	 * 修改数据状态
	 */
	public void statedata() {
		int state=request.getParameterInt("state");
		
		StateEnum[] states = StateEnum.values();
		boolean r= false;
		for (StateEnum stateEnum : states) {
			if(stateEnum.ordinal()==state) r=true;
		}
		if(!r)
		{
			run_false("请求参数错误！");return;
		}
		Data bean =new Data().get(ask.getId());
		if(bean==null)
		{
			run_false("数据不存在！");return;
		}
		Urldata urldata = new Urldata().get(bean.getUrldata());
		if(urldata==null) {
			run_false("数据不存在！");return;
		}
		urldata.setState(state);
		urldata.update();
		run_success("修改成功！");
	}
	
	public void deletedata() {
		Data bean =new Data().get(ask.getId());
		if(bean==null)
		{
			run_false("数据不存在！");return;
		}
		bean.delete();
		run_success("删除成功！");
	}
	
	public void updatedata() {
		Data data = getAsk(Data.class);			
		Data bean =new Data().get(ask.getId());
		if(bean==null)
		{
			run_false("数据不存在！");return;
		}
		if(StringUtil.isNotEmpty(data.getValue()))bean.setValue(data.getValue());
		if(data.getSeq()>0||data.getSeq()!=bean.getSeq())bean.setSeq(data.getSeq());		
		if(StringUtil.isNotEmpty(data.getEqual()))bean.setEqual(data.getEqual());
		long id = bean.update();
		if(id>0)
		{
			run_success("更新成功！");return;
		}else
		{
			run_false("更新失败！");return;
		}

		
	}
	
	public void updateml() {
		Ml ml=getAsk(Ml.class);
		Ml bean=new Ml().get(ask.getId());
		if(bean==null)
		{
			run_false("数据不存在！");return;
		}
		if(StringUtil.isNotEmpty(ml.getKeyword()))bean.setKeyword(ml.getKeyword());
		if(ml.getType()>0||ml.getType()!=bean.getType())bean.setType(ml.getType());
		if(ml.getIsnull()>0||ml.getIsnull()!=bean.getIsnull())bean.setIsnull(ml.getIsnull());
		if(ml.getMinlength()>0||ml.getMinlength()!=bean.getMinlength())bean.setMinlength(ml.getMinlength());
		if(ml.getMaxlength()>0||ml.getMaxlength()!=bean.getMaxlength())bean.setMaxlength(ml.getMaxlength());
		if(ml.getMinword()>0||ml.getMinword()!=bean.getMinword())bean.setMinword(ml.getMinword());
		if(ml.getFilterword()>0||ml.getFilterword()!=bean.getFilterword())bean.setFilterword(ml.getFilterword());
		long id = bean.update();
		if(id>0)
		{
			run_success("更新成功！");return;
		}else
		{
			run_false("更新失败！");return;
		}
		
		
	}
	@SuppressWarnings("serial")
	public void updatetag() {
		
		Tag tag=getAsk(Tag.class);
		if(tag.getId()>0)
		{
			Tag bean=new Tag().get(ask.getId());
			if(bean==null)
			{
				run_false("数据不存在！");return;
			}
			if(tag.getValid()>0||tag.getValid()!=bean.getValid()){
				if(tag.getValid()==1)
				{
					TagService service = new TagService();
					service.updateTag(bean);
				}
				bean.setValid(tag.getValid());
			}
			long id = bean.update();
			if(id>0)
			{
				
				run_success("更新成功！");return;
			}else
			{
				run_false("更新失败！");return;
			}
		}else
		{
			if(isEmpty(tag.getName()))
			{
				run_false("请求参数错误！");return;
			}
			Tag bean = new Tag().get(new HashMap<String ,Object>() {{put("name", tag.getName());}});
			if(bean!=null) {
				run_false("已有标签！");return;
			}
			tag.setValid(ValidEnum.认证.ordinal());
			long id = tag.save();
			if(id>0) {
				run_success("添加成功！");return;
			}else
			{
				run_false("添加失败！");return;
			}
		}
	
	}
	
	/**
	 * 上传数据
	 */
	public void updata() {
		
		Data data = new Data().get(ask.getId());
		if(data==null)
		{
			run_false("数据不存在！");return;
		}
		DataService service = new DataService();
		StateEnum state=service.upload(data);
		run_success(state.name());
	}
	
	
	
	
	public void test() {
		
		JSONObject json = request.getJson();
		log.info("取得JSON数据:{}",json);
		
		run_success(StateEnum.上传完成.name());
	}
	
}
